package es.ats.codington.web.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.PlaceServiceImpl;
import es.ats.codington.festival.services.VisitorServiceImpl;

/**
 * Servlet implementation class RegisterEvent.
 */
@WebServlet("/LoginVisitorServlet")
public class LoginVisitorServlet extends HttpServlet {
   // private static final long serialVersionUID = 1L;
       
    /** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/**
	 * Instantiates a new login visitor servlet.
	 *
	 * @see HttpServlet#HttpServlet()
	 */
    public LoginVisitorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * Do get.
     *
     * @param request the request
     * @param response the response
     * @throws ServletException the servlet exception
     * @throws IOException Signals that an I/O exception has occurred.
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    
    }

    /**
     * Do post.
     *
     * @param request the request
     * @param response the response
     * @throws ServletException the servlet exception
     * @throws IOException Signals that an I/O exception has occurred.
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            Visitor visitor = new Visitor();
            VisitorServiceImpl vis = new VisitorServiceImpl();
       
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            
            visitor = vis.loginVisitor(username, password);
           
             if((visitor.getVisitorId()!=0)&&(visitor.getPassword().equals(password))){
            	 HttpSession session = request.getSession(true);
            	 Cookie loginCookie = new Cookie("visitor",username);
            	 
            	 String[] addressArray = visitor.getAddress().split("\\.");
            	 String address = "";
            	 
            	 for (int i = 0; i < (addressArray.length)-1; i++ ){
            		 address = address + addressArray[i];
            	 } 

            	 //java.util.Arrays.toString(testString.split("\\."))
            	 session.setAttribute("Session", 1);
            	 session.setAttribute("VisitorID", visitor.getVisitorId());
            	 session.setAttribute("Admin", visitor.isAdmin());
            	 session.setAttribute("VisitorAge", visitor.getAge());
            	 Cookie nameCookie = new Cookie("VisitorName", visitor.getName());
            	 Cookie lastnameCookie = new Cookie("VisitorLastName", visitor.getLastName());
            	 Cookie ageCookie = new Cookie("VisitorAge", Integer.toString(visitor.getAge()));
            	 Cookie addressCookie = new Cookie("VisitorAddress", visitor.getAddress());
            	 Cookie phoneCookie = new Cookie("VisitorPhone", visitor.getPhoneNumber());
            	 
            	 response.addCookie(loginCookie);
            	 response.addCookie(nameCookie);
            	 response.addCookie(lastnameCookie);
            	 response.addCookie(ageCookie);
            	 response.addCookie(addressCookie);
            	 response.addCookie(phoneCookie);           	 
            
              	 //setting cookie to expiry in 30 mins
            	 //loginCookie.setMaxAge(30*60);
            	 
            	 if(visitor.isAdmin()){
        			 PlaceServiceImpl pla = new PlaceServiceImpl(); 

        			 ArrayList<Integer> place = new ArrayList<Integer>();
        					 
        			 place=pla.obtainAllPlaces();
        			 
        			 session.setAttribute("Places", place);
        			 session.setAttribute("Admin", 1);
        			 //response.sendRedirect("homeAdmin.jsp");
        			 ListAllEventServlet les = new ListAllEventServlet();
                     les.sendListAllEvent(request, response);
        			 
            	 }else{
            		 //response.sendRedirect("home.jsp");
            		 
            		 //RequestDispatcher rd=request.getRequestDispatcher("home.jsp");  
                     //rd.forward(request,response); 
            		 session.setAttribute("Admin", 0);
                     ListEventServlet les = new ListEventServlet();
                     les.sendListEventvisitor(request, response); 
                     
                    
            	 }
            	 
 
             }
             else{
            	 response.sendRedirect("login.jsp");
            	 //*RequestDispatcher rd=request.getRequestDispatcher("home.jsp");  
                 //*rd.include(request,response); 
             }           
        }catch(Exception theException){
             System.out.println(theException); 
        }
    }

}
